<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>Hello APP</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../css/cutom-icon/iconfont.css">
	<style>

	</style>
</head>
<body>
    <section class="aui-content">
      <ul class="aui-list aui-media-list aui-margin-b-10 aui-margin-t-10">
          <li class="aui-list-item aui-list-item-middle" onclick="openActionsheet();">
              <div class="aui-media-list-item-inner">
                  <div class="aui-list-item-inner">
                      群组头像
                  </div>
                  <div class="aui-list-item-media" style="width: 3rem;" id="img_id">
                      <img src="../image/users.png" class="aui-img-round aui-list-img-sm">
                  </div>
              </div>
          </li>
      </ul>

      <ul class="aui-list aui-list-in">
            <li class="aui-list-item" onclick="openDialog()" id="groupName_id">
                <div class="aui-list-item-inner aui-list-item-arrow">
                    <div class="aui-list-item-title">群名称</div>
                    <div class="aui-list-item-right" id="groupNameId">

                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-title">创建人</div>
                    <div class="aui-list-item-right" id="createNameId"></div>
                </div>
            </li>
            <li class="aui-list-item" id="memberId" onclick="openWin('daily_mail')">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-title">邀请新成员</div>
                    <div class="aui-list-item-right" id="InvitationId">
                    <i class="iconfont icon-tianjia"></i>
                    <!--<input type="hidden" id="member_id" value=""/>-->
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner aui-list-item-arrow">
                    <div class="aui-list-item-title">查看聊天内容</div>
                </div>
            </li>
        </ul>
    </section>
    <div id="logout_id" class="aui-content aui-margin-15">
    <div class="aui-btn aui-btn-danger aui-btn-block aui-btn-sm " onclick="logout()">退出群组</div>
    </div>

</body>
<script type="text/javascript" src="../script/zepto.min.js" ></script>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/aui-actionsheet.js" ></script>
<script type="text/javascript" src="../script/aui-dialog.js" ></script>
<script type="text/javascript">
    var dialog = new auiDialog();
    var actionsheet = new auiActionsheet();

    apiready = function(){
        var targetId = api.pageParam.targetId;
        var username = $api.getStorage('username');
        api.parseTapmode();
        api.ajax({
            url: ServeUrl + "m=Index&a=groupInfo",
            method: 'post',
            data: {
                values: {
                    targetId:targetId
                }
            }
        }, function(ret, err) {
            if(ret.status==1){
                $("#groupNameId").html(ret.data.groupName);
                $("#createNameId").html(ret.data.cn_name);
                if(ret.data.img_file!==null){
                    var html = ''
                    html+='<img src="'+ServeIp+'include/data/headImg/'+ret.data.img_file+'" class="aui-img-round aui-list-img-sm">';
                    $("#img_id").html(html);
                }
                //$("#member_id").val(ret.data.member);
                $api.setStorage('groupMember', ret.data.member);

                if(username !== ret.data.createname){
                    $("#memberId").hide();
                    $("#groupName_id").removeAttr("onclick");
                }
            }
        });
    };


    //打开底部弹窗
    function openActionsheet(){
        actionsheet.init({
            frameBounces:true,//当前页面是否弹动，（主要针对安卓端）
            title:"更换头像",
            cancelTitle:'取消',
            //destructiveTitle:'红色警告按钮',
            buttons:['拍照','图库选择']
        },function(ret){
            if(ret){
                if(ret.buttonIndex=="1"){
                    getPic("camera");
                }else {
                    getPic("library");
                }
            }
        })
    }

    //选择图片
    function getPic(type){
        api.getPicture({
            sourceType: type,
            encodingType: 'jpg',
            mediaValue: 'pic',
            destinationType: 'url',
            allowEdit: true,
            quality: 100,
            targetWidth: 300,
            targetHeight: 300,
            saveToPhotoAlbum: false
        }, function(ret, err) {
            if (ret) {
                var img_file = ret.data;
                var targetId = api.pageParam.targetId;
                api.ajax({
                    url: ServeUrl + "m=Index&a=updateGroupInfo",
                    method: 'post',
                    data: {
                        values: {
                            targetId:targetId
                        },
                        files: {
                            file: ret.data,
                        }
                    }
                }, function(ret, err) {
                    var html = ''
                    html+='<img src="'+img_file+'" class="aui-img-round aui-list-img-sm">';
                    $("#img_id").html(html);
                });
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    //修改群组名称
    function openDialog(){
        dialog.prompt({
            title:"修改群名称",
            text:'请输入群名称',
            buttons:['取消','确定']
        },function(ret){
            if(ret.buttonIndex == 2){
                var targetId = api.pageParam.targetId;
                var groupName = ret.text;
                alert(targetId);
                alert(groupName);
                api.ajax({
                    url: ServeUrl + "m=Index&a=updateGroupInfo",
                    method: 'post',
                    data: {
                        values: {
                            groupName: ret.text,
                            targetId:targetId
                        }
                    }
                }, function(ret, err) {
                    api.sendEvent({
                        name:'setDiscussionName',
                        extra:{
                            targetId:''+targetId+'',
                            groupName: groupName
                        }
                    })
                });
            }
        })
    }

    //退出群组
    function logout(){
        var targetId = api.pageParam.targetId;
        var username = $api.getStorage("username");
        api.ajax({
            url: ServeUrl + "m=Index&a=quitDiscussion",
            method: 'post',
            data: {
                values: {
                    username: username,
                    targetId:targetId
                }
            }
        }, function(ret, err) {
            api.sendEvent({
                name:'quitDiscussion',
                extra:{
                    targetId:''+targetId+'',
                }
            })
        });
    }

    function openWin(name) {
        var member = $api.getStorage('groupMember');
        //alert(member);
        api.openWin({
            name: name,
            url: name+'_win.html',
            pageParam: {
                winName: "group_set",
                frameName:"group_set_frm",
                member:member
            }
        });
    }

    function selectUsers(val,member){
        var targetId = api.pageParam.targetId;
        //var usersIdarr = val.split(",");
        //var usersIdstr=usersIdarr.join('","');
        //usersIdstr = '["'+usersIdstr+'"]';
        api.ajax({
            url: ServeUrl + "m=Index&a=getUsers",
            method: 'post',
            data: {
                values: {
                    usersId: val
                }
            }
        }, function(ret, err) {
            //alert(JSON.stringify(ret));
            if(ret.status == 1){

                var   usersIdArr = new Array;
                for (var i = 0; i < ret.data.length; i++) {
                    usersIdArr[i] = ret.data[i].ryId;
                }
                var usersIdstr=usersIdArr.join('","');
                usersIdstr = '["'+usersIdstr+'"]';

                api.sendEvent({
                    name:'addMemberToDiscussion',
                    extra:{
                        targetId:''+targetId+'',
                        usersIdstr:usersIdstr,
                        val:val+','+member
                    }
                })
            }
        })

    }
</script>
</html>
